#include "CSkillData.h"
#include "TinyXML.h"
#include "UtilityFunctions.h"



CSkillData::CSkillData( void )
{
}

CSkillData::~CSkillData( void )
{
}

BOOL CSkillData::OnLoadSkillData( std::tstring strFileName )
{
	if( strFileName.length() <= 0 ) return FALSE;

	TiXmlDocument Doc;
#ifdef UNICODE
	std::string strAnsiFileName = CodeUtil::UniCodeToAnsi( strFileName.c_str() );
	if( !Doc.LoadFile( strAnsiFileName.c_str() ) ) return FALSE;
#else
	if( !Doc.LoadFile( strFileName.c_str() ) ) return FALSE;
#endif

	TiXmlNode* pRoot = Doc.FirstChild( "SkillDataList" );
	if( !pRoot ) return FALSE;

	TiXmlNode* pSkillData = pRoot->FirstChild( "SkillData" );
	while( pSkillData )
	{
		stSkillData NewSkillData;

		NewSkillData.m_nSkillID = atoi( pSkillData->ToElement()->Attribute( "ID" ) );
		NewSkillData.m_strSkillName = CodeUtil::AnsiToUniCode( pSkillData->ToElement()->Attribute( "Name" ) );
		NewSkillData.m_nSkillType = atoi( pSkillData->ToElement()->Attribute( "Type" ) );

		TiXmlNode* pDamage = pSkillData->FirstChild( "SkillDamage" );
		while( pDamage )
		{
			stDamageData NewDamageData;

			NewDamageData.m_eDamageType = ( eDamageType )atoi( pDamage->ToElement()->Attribute( "Type" ) );
			NewDamageData.m_fDamage = ( float )atof( pDamage->ToElement()->Attribute( "Damage" ) );
			
			NewSkillData.m_vecDamage.Add( NewDamageData );
			pDamage = pDamage->NextSibling();
		}

		TiXmlNode* pBuff = pSkillData->FirstChild( "SkillBuff" );
		while( pBuff )
		{
			stSkillBuffData NewSkillBuffData;

			NewSkillBuffData.m_nBuffID = atoi( pDamage->ToElement()->Attribute( "BuffID" ) );
			NewSkillBuffData.m_nDuration = atoi( pDamage->ToElement()->Attribute( "Duration" ) );

			NewSkillData.m_vecBuff.Add( NewSkillBuffData );
			pBuff = pBuff->NextSibling();
		}

		NewSkillData.m_eRangeType = ( eAttackRangeType )atoi( pSkillData->ToElement()->Attribute( "RangeType" ) );
		NewSkillData.m_nTargetType = atoi( pSkillData->ToElement()->Attribute( "TargetType" ) );
		NewSkillData.m_nMaxTargetCount = atoi( pSkillData->ToElement()->Attribute( "MaxTarget" ) );

		NewSkillData.m_MissileData.m_eMissileType = ( eMissileType )atoi( pSkillData->ToElement()->Attribute( "MissileType" ) );
		NewSkillData.m_MissileData.m_nMissileCount = atoi( pSkillData->ToElement()->Attribute( "MissileCount" ) );
		NewSkillData.m_MissileData.m_fMissileSpeed = ( float )atof( pSkillData->ToElement()->Attribute( "MissileSpeed" ) );
		NewSkillData.m_MissileData.m_nMissileLifeTime = atoi( pSkillData->ToElement()->Attribute( "MissileLifeTime" ) );

		m_mapSkillData.Add( NewSkillData.m_nSkillID, NewSkillData );
		pSkillData = pSkillData->NextSibling();
	}

	return TRUE;
}

int CSkillData::GetSkillIDByIndex( int nIndex )
{
	stSkillData* pSkillData = m_mapSkillData.GetByIndex( nIndex );
	return pSkillData ? pSkillData->m_nSkillID : -1;
}

const TCHAR* CSkillData::GetSkillName( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_strSkillName.c_str() : NULL;
}

int CSkillData::GetSkillType( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_nSkillType : 0;
}

int CSkillData::GetSkillDamageCount( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_vecDamage.GetSize() : 0;
}

eDamageType CSkillData::GetSkillDamageType( int nSkillID, int nIndex )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	if( !pSkillData ) return DamageType_UnKnown;

	stDamageData* pDamageData = pSkillData->m_vecDamage.Get( nIndex );
	return pDamageData ? pDamageData->m_eDamageType : DamageType_UnKnown;
}

float CSkillData::GetSkillDamage( int nSkillID, int nIndex )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	if( !pSkillData ) return 0.0f;

	stDamageData* pDamageData = pSkillData->m_vecDamage.Get( nIndex );
	return pDamageData ? pDamageData->m_fDamage : 0.0f;
}

int CSkillData::GetSkillBuffCount( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_vecBuff.GetSize() : 0;
}

int CSkillData::GetSkillBuffID( int nSkillID, int nIndex )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	if( !pSkillData ) return -1;

	stSkillBuffData* pSkillBuffData = pSkillData->m_vecBuff.Get( nIndex );
	return pSkillBuffData ? pSkillBuffData->m_nBuffID : -1;
}

__int64	CSkillData::GetSkillBuffDuration( int nSkillID, int nIndex )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	if( !pSkillData ) return 0;

	stSkillBuffData* pSkillBuffData = pSkillData->m_vecBuff.Get( nIndex );
	return pSkillBuffData ? pSkillBuffData->m_nDuration : 0;
}

eAttackRangeType CSkillData::GetSkillRangeType( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_eRangeType : AttackRangeType_UnKnown;
}

int CSkillData::GetSkillTargetType( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_nTargetType : 0;
}

int CSkillData::GetSkillMaxTargetCount( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_nMaxTargetCount : 0;
}

eMissileType CSkillData::GetSkillMissileType( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_MissileData.m_eMissileType : MissileType_UnKnown;
}

int CSkillData::GetSkillMissileCount( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_MissileData.m_nMissileCount : 0;
}

float CSkillData::GetSkillMissileSpeed( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_MissileData.m_fMissileSpeed : 0.0f;
}

__int64 CSkillData::GetSkillMissileLifeTime( int nSkillID )
{
	stSkillData* pSkillData = _GetSkillData( nSkillID );
	return pSkillData ? pSkillData->m_MissileData.m_nMissileLifeTime : 0;
}

stSkillData* CSkillData::_GetSkillData( int nSkillID )
{
	return m_mapSkillData.Get( nSkillID );
}
